﻿(function ($) {
    $.widget("zeus.editListWidget", {
        // default options
        options: {
            edtTextId: null,
            edtSelId: null,
            dataSource: [],
            pageIndex: 1,
            pageNum: 1
            // callbacks
        },

        // the constructor
        _create: function () {
            var that = this;

            // append editListBox
            var listBoxHtml = "<div class='editListBox'><input type='text' id='" + this.options.edtTextId + "' class='shortList' />"
                + "<ul class='editList' id='" + this.options.edtSelId + "'></ul></div>";
            this.element.append(listBoxHtml);

            // subscribe edit text box click event
            that.subscribeEditTextClick();

            // subscribe edit select li click event
            that.subscribeSelectItemClick();

            // subscribe edit text box lost focus event
            //that.subscribeEditTextBlur();
        },

        // clear select items
        clearSelectItems: function () {
            $('#' + this.options.edtSelId).html("");
        },

        // init select items
        initSelectItems: function () {
            var editTextContent = $('#' + this.options.edtTextId).val();
            var selItem = "";
            var i = 0;
            //显示的数量
            var count = this.options.pageIndex * this.options.pageNum;

            $.each(this.options.dataSource, function (index, value) {
                var name = value.name;
                //if ((!editTextContent || name.indexOf(editTextContent) == 0) && i < count) {
                if (i < count) {
                    selItem += "<li>" + name + "</li>"
                    i++;
                }
            });

            if (i == count) {
                selItem += "<li class='more'>more</li>";
            }

            $('#' + this.options.edtSelId).html(selItem);
        },

        // edit text box click event
        subscribeEditTextClick: function () {
            var that = this;

            $('#' + this.options.edtTextId).bind('keyup focus', function (event) {
                //ZEUS.DEBUG.log("edit text box selected");
                that.initSelectItems();
            });
        },

        // edit text box lost focus event
        subscribeEditTextBlur: function () {
            var that = this;

            $('#' + this.options.edtTextId).blur(function (event) {
                that.clearSelectItems();
            });
        },

        // edit select li click event
        subscribeSelectItemClick: function () {
            var that = this;

            $(document).on('click', '#' + this.options.edtSelId + ' li', function (e) {
                if (e.target.className == "more") {
                    that.options.pageIndex++;
                    that.initSelectItems();
                } else {
                    $('#' + that.options.edtTextId).val(e.target.innerHTML);
                    $('#' + that.options.edtSelId).html("");
                }

            });

        },

        // destructor
        destroy: function () {
            $.Widget.prototype.destroy.call(this);
        }
    });
})(jQuery)
